package com.typing.battle.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.typing.battle.R;
import com.typing.battle.listener.OnDataSyncCompleteListener;
import com.typing.battle.util.PreferenceMgr;
import com.typing.battle.util.ServerComunicationUtil;
import com.typing.battle.value.ServerComunicationDTO;

public class TypingBattleChatRoomCreateActivity  extends TypingBattleRootActivity {
    
	private String _ID;
	private Button create,back;
	private Spinner people,chatType;
	private EditText chatName;
	private String peopleStr="2",chatTypeStr="0";
	public Context mContext;
	
	private PreferenceMgr mgr;
    @Override
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.battle_chat_room_create);	     
        mContext = this;
       
        mgr = PreferenceMgr.getInstance(this);
        chatName = (EditText)findViewById(R.id.input_room_name);
        
		people = (Spinner)findViewById(R.id.spinner_room_people);
		final String[] peopleArray =getResources().getStringArray(R.array.chat_room_people);
		ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
                this, R.array.chat_room_people, android.R.layout.simple_spinner_item);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        people.setAdapter(adapter1);
        people.setOnItemSelectedListener(
                new OnItemSelectedListener() {
                    public void onItemSelected(
                            AdapterView<?> parent, View view, int position, long id) {
                        //showToast("Spinner1: position=" + position + " id=" + id);
                    	
                    	peopleStr = peopleArray[position];
                    }

                    public void onNothingSelected(AdapterView<?> parent) {
                        //showToast("Spinner1: unselected");
                    }
                });
		chatType = (Spinner)findViewById(R.id.spinner_room_type);
		ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
                this, R.array.chat_room_type, android.R.layout.simple_spinner_item);
		adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		chatType.setAdapter(adapter2);
		chatType.setOnItemSelectedListener(
                new OnItemSelectedListener() {
                    public void onItemSelected(
                            AdapterView<?> parent, View view, int position, long id) {
//                        Toast.makeText(mContext,"Spinner1: position=" + position + " id=" + id,Toast.LENGTH_SHORT).show();
                    	chatTypeStr = ""+(position+1);
                    }

                    public void onNothingSelected(AdapterView<?> parent) {
                        //showToast("Spinner1: unselected");
                    }
                });
		
		create = (Button)findViewById(R.id.btn_room_create);
		create.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ServerComunicationDTO dto = new ServerComunicationDTO();
				dto.setActionType(ServerComunicationUtil.TYPE_CHAT_ROOM_CREATE);
				dto.setResponseType(ServerComunicationUtil.RETURN_TYPE_STRING);
				setChatRoomData(dto);
				
				try{
					ServerComunicationUtil.serverAction(dto, mContext,TypingBattleChatRoomCreateActivity.this);
				}catch(Exception e){
					Log.e("", "TYPE_CHAT_ROOM_CREATE ERROR ::: "+e.getMessage());
				}
			}
		});
		back = (Button)findViewById(R.id.btn_back);
		back.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext, TypingBattleChatRoomListActivity.class);
				intent.putExtra("RESULT", false);		
				setResult(TypingBattleChatRoomListActivity.CHAT_ROOM_CREATE,intent);
				finish();
			}
		});
    } 
    public void setChatRoomData(ServerComunicationDTO dto){
    	chatName = (EditText)findViewById(R.id.input_room_name);
    	dto.setChatRoomName(chatName.getText().toString());
    	Log.d("chatRoomName",chatName.getText().toString());
    	dto.setChatRoomOwner(mgr.getData(PreferenceMgr.USER_EMAIL_KEY, ""));
    	dto.setChatRoomType(chatTypeStr);
    	dto.setMaxPeopleCount(peopleStr);
    }
    
	@Override
	public void onSyncComplete(int resultCode,ServerComunicationDTO dto, Object resultObj) {
		if(resultCode == OnDataSyncCompleteListener.RESULT_SYNC_FAIL){
			if(resultObj!=null)
				ServerComunicationUtil.showNotify((String)resultObj, this);
		}else{
			if(dto.getActionType() == ServerComunicationUtil.TYPE_CHAT_ROOM_CREATE){
				String result = null;
				try{
					result = (String)resultObj;
				}catch(Exception e){
					
				}
				Intent intent = new Intent(mContext, TypingBattleChatRoomListActivity.class);
				intent.putExtra("RESULT", true);
				intent.putExtra("CHAT_ROOM_ID", result);
				setResult(TypingBattleChatRoomListActivity.CHAT_ROOM_CREATE,intent);
				finish();
			}		
		}
		super.onSyncComplete(resultCode, dto, resultObj);
	}
    
}